﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskBasedSchedule.Core
{
    public class Role
    {
        //TODO: Mehtods in this class IS NOT thread-safe.

        private HashSet<string> _authorityKeys;

        public int Id { get; internal set; }

        public string Name { get; internal set; }

        public string[] GetAuthorities()
        {
            return _authorityKeys.ToArray<string>();
        }

        public bool HasAuthority(string key)
        {
            bool result = _authorityKeys.Contains(key);

            return result;
        }

        internal Role()
        {
            _authorityKeys = new HashSet<string>();
        }

        internal void SetAuthorities(string data)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[{][a-z|A-Z|\\d+-_]+[}]");

            _authorityKeys.Clear();

            var results = regex.Matches(data);
            foreach(var result in results)
            {
                _authorityKeys.Add(((string)result).Substring(1, ((string)result).Length - 2));
            }

            return;
        }
    }
}